<!--
  * 订单明细
  *
-->
<template>
  <a-table :dataSource="props.orderData" :columns="columns" :pagination="false" :scroll="{ x: 'max-content' }" rowKey="id" size="small" bordered>
    <template #bodyCell="{ text, record, index, column }">
      <template v-if="column.dataIndex === 'index'">
        {{ index + 1 }}
      </template>
      <template v-if="column.dataIndex === 'source_order_no'">
        <CopyText :text="text" />
      </template>
      <!-- <template v-if="column.dataIndex === 'action'">
        <div class="smart-table-operate">
          <a-button @click="cancelPick(record)" danger type="link" v-privilege="'goods:delete'" v-if="[PICKORDER_STATUS_ENUM.PENDING_PICK.value, PICKORDER_STATUS_ENUM.PICKING.value].includes(record.status) && getCompanyInfo.out_strategy === OUT_STRATEGY.SCHEDULE_PICK_LOAD.value"> 取消拣货 </a-button>
        </div>
      </template> -->
    </template>
    <template #summary>
      <a-table-summary fixed>
        <a-table-summary-row>
          <a-table-summary-cell>总计</a-table-summary-cell>
          <a-table-summary-cell>{{ props.orderData.length }}单</a-table-summary-cell>
          <a-table-summary-cell />
          <a-table-summary-cell />
          <a-table-summary-cell>{{ props.detailData.total_num }}</a-table-summary-cell>
          <a-table-summary-cell>{{ total_price }}</a-table-summary-cell>
          <a-table-summary-cell />
          <a-table-summary-cell />
          <a-table-summary-cell />
          <a-table-summary-cell />
        </a-table-summary-row>
      </a-table-summary>
    </template>
  </a-table>
</template>
<script setup lang="ts">
import _ from 'lodash'
import { message, Modal } from 'ant-design-vue'
import { useEnums } from '@/hooks/plugins/useEnums'
import { pickOrderApi } from '@/api/business/storage/picking-api'
import { PICKORDER_STATUS_ENUM } from '@/constants/business/storage/picking/picking-const'
import { OUT_STRATEGY } from '@/constants/business/storage/process-rules/process-rules-const'
import { useUserStore } from '@/store/modules/user-store'
const { getDescByValue } = useEnums()
const { getCompanyInfo } = useUserStore()
const emit = defineEmits(['refresh'])
const props = defineProps({
  detailData: {
    type: Object,
    default: () => ({})
  },
  orderData: {
    type: Array,
    default: () => []
  }
})

const columns = reactive([
  {
    title: '序号',
    dataIndex: 'index',
    width: 20
  },
  {
    title: '源单号',
    dataIndex: 'source_order_no'
  },
  {
    title: '门店名称',
    dataIndex: 'store_name'
  },
  {
    title: '订单类型',
    dataIndex: 'related_type_text'
  },
  {
    title: '订单数量',
    dataIndex: 'order_num'
  },
  {
    title: '订单金额',
    dataIndex: 'total_price'
  },
  {
    title: '业务员',
    dataIndex: 'salesman'
  },
  {
    title: '开单仓库',
    dataIndex: 'warehouse'
  },
  {
    title: '单据日期',
    dataIndex: 'bill_date'
  },
  {
    title: '备注',
    dataIndex: 'remark'
  }
  // {
  //   title: '操作',
  //   dataIndex: 'action'
  // }
])

const total_price = computed(() => {
  return _(props.orderData)
    .map(({ total_price }) => Number(total_price))
    .sum()
    .toFixed(2)
})

// ---------------------------- 取消拣货 ----------------------------
function cancelPick(record) {
  Modal.confirm({
    title: '确认取消拣货单？',
    onOk: async () => {
      try {
        const res = await pickOrderApi.cancel({ id: props.detailData.id, detail_id: record.id })
        message.success(res.msg)
        emit('refresh')
      } catch (error) {
        message.error(error.msg)
      } finally {
        return Promise.resolve()
      }
    }
  })
}
</script>
